const http = require('../../network/http')
const api = require('../../network/api')
var util = require('../../utils/dateTime.js');
// pages/census/census.js

import * as echarts from '../../libs/ec-canvas/echarts';
// 饼图 option
let optionPie = {
  // 初始化数据
  color: [ '#5AD8A6', '#FAC858', '#fc6f6f','#6bf3f8','#f06ffc'], 
  series: [{
    name: '访问来源',
    type: 'pie',
    radius: ['45%', '70%'],
    avoidLabelOverlap: false,
    label: {
      show: false,
      position: 'center',
    },
    emphasis: {
      label: {
        show: true,
        fontSize: '20',
        fontWeight: 'bold',
      },
    },
    labelLine: {
      show: false,
    },
    data: [{
        value: 335,
        name: '微信充值'
      },
      {
        value: 310,
        name: '微信订单'
      },
      {
        value: 390,
        name: '余额支付'
      },
      {
        value: 390,
        name: '抖音券'
      },
    ],
    itemStyle: {
      emphasis: {
        shadowBlur: 10,
        shadowOffsetX: 0,
        shadowColor: 'rgba(0, 0, 0, 0.5)',
      },
    },
  }, ],
};
let optionBar = {
  xAxis: {
    data: ['微信支付', '美团券', '抖音券', '平台券', '余额支付'],
    axisLabel: {
      interval: 0,    //强制文字产生间隔
      // rotate: 45,     //文字逆时针旋转45°
      textStyle: {    //文字样式
        color: "black",
        fontSize: 8,
        fontFamily: 'Microsoft YaHei'
      },
    }  
  },
  yAxis: {
    axisLabel: {
      interval: 0,    //强制文字产生间隔
      // rotate: 45,     //文字逆时针旋转45°
      textStyle: {    //文字样式
        color: "black",
        fontSize: 8,
        fontFamily: 'Microsoft YaHei'
      },
    }  
  },
  series: [
    {
      type: 'bar',
      label: {
        show: false,
        position: 'top',
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '12',
          fontWeight: 'bold',
          color: '#000000',
        },
      },
      data: [1,2,3,4,5],
      stack: 'x',
      itemStyle: {
        color: '#5AD8A6',
        shadowColor: '#5AD8A6',
        borderType: 'dashed',
        opacity: 0.9,
      }
    },
    {
      type: 'bar',
      label: {
        show: false,
        position: 'top',
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '12',
          fontWeight: 'bold',
          color: '#000000',
        },
      },
      data: [2,2,3,2,2],
      stack: 'x',
      itemStyle: {
        color: '#fcda43',
        shadowColor: '#fcda43',
        borderType: 'dashed',
        opacity: 0.9,
      }
    },
    {
      type: 'bar',
      label: {
        show: false,
        position: 'top',
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '12',
          fontWeight: 'bold',
          color: '#000000',
        },
      },
      data: [2,2,3,2,2],
      stack: 'x',
      itemStyle: {
        color: '#fc6f6f',
        shadowColor: '#fc6f6f',
        borderType: 'dashed',
        opacity: 0.9,
      }
    },
    {
      type: 'bar',
      label: {
        show: false,
        position: 'top',
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '12',
          fontWeight: 'bold',
          color: '#000000',
        },
      },
      data: [2,2,3,2,2],
      stack: 'x',
      itemStyle: {
        color: '#6bf3f8',
        shadowColor: '#6bf3f8',
        borderType: 'dashed',
        opacity: 0.9,
      }
    },
    {
      type: 'bar',
      label: {
        show: false,
        position: 'top',
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '12',
          fontWeight: 'bold',
          color: '#000000',
        },
      },
      data: [2,2,3,2,2],
      stack: 'x',
      itemStyle: {
        color: '#f06ffc',
        shadowColor: '#f06ffc',
        borderType: 'dashed',
        opacity: 0.9,
      }
    }
  ]
};
let chartPie;
// 图表初始化
function initChartPie(canvas, width, height, dpr) {
  chartPie = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr, // 像素
  });
  canvas.setChart(chartPie);
  chartPie.setOption(optionPie);
  return chartPie;
}
let chartBar;
// 图表初始化
function initChartBar(canvas, width, height, dpr) {
  chartBar = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr, // 像素
  });
  canvas.setChart(chartBar);
  chartBar.setOption(optionBar);
  return chartBar;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ecPie: {
      onInit: initChartPie,
    },
    ecBar: {
      onInit: initChartBar,
    },
    storeName: '',
    storeId: '',
    chooseMonth: '2022-10',
    toMonth: '',
    chooseYear: '',
    toYear: '',
    dateType: 1, //1月度2年度
    managerList: {},
    sendDate: '',
    wePayTradeRatio: '',
    wePayRechargeRatio: '',
    douYinRatio: '',
    balanceRatio: '',
    meiTuanRatio: '',
    platformCouponRatio: '',
    showDetail: false,
    detailList: [{
      date: '20',
      memberRecharge: '131',
      realWePaySales: '897',
      balanceSales: '3123',
      douYinSales: '4654',
      total: '3132465'
    },{
      date: '19',
      memberRecharge: '131',
      realWePaySales: '897',
      balanceSales: '3123',
      douYinSales: '4654',
      total: '3132465'
    },{
      date: '18',
      memberRecharge: '131',
      realWePaySales: '897',
      balanceSales: '3123',
      douYinSales: '4654',
      total: '3132465'
    },{
      date: '17',
      memberRecharge: '131',
      realWePaySales: '897',
      balanceSales: '3123',
      douYinSales: '4654',
      total: '3132465'
    },{
      date: '16',
      memberRecharge: '131',
      realWePaySales: '897',
      balanceSales: '3123',
      douYinSales: '4654',
      total: '31465'
    },{
      date: '15',
      memberRecharge: '1310',
      realWePaySales: '897',
      balanceSales: '3123',
      douYinSales: '4654',
      total: '3132465'
    },],
  },
  getData : function(e){
    http.request({
      showLoading: true,
      url: api.getBusinessSales,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        date: this.data.sendDate,
        mode: this.data.dateType,
      },
    }).then(res => {
      if (res.data.code == 200) {
        console.log("订单列表数据返回:");
        console.log(res.data.result);
        var wePayTradeRatio = parseFloat(res.data.result.revenueTypeRatio.wePayTradeRatio * 100).toFixed(0);
        var wePayRechargeRatio = parseFloat(res.data.result.revenueTypeRatio.wePayRechargeRatio * 100).toFixed(0);
        var douYinRatio = parseFloat(res.data.result.revenueTypeRatio.douYinRatio * 100).toFixed(0);
        var balanceRatio = parseFloat(res.data.result.revenueTypeRatio.balanceRatio * 100).toFixed(0);
        var meiTuanRatio = parseFloat(res.data.result.revenueTypeRatio.meiTuanRatio * 100).toFixed(0);
        var platformCouponRatio = parseFloat(res.data.result.revenueTypeRatio.platformCouponRatio * 100).toFixed(0);
        this.setData({
          'managerList': res.data.result,
          wePayTradeRatio: wePayTradeRatio,
          wePayRechargeRatio: wePayRechargeRatio,
          douYinRatio: douYinRatio,
          balanceRatio: balanceRatio,
          meiTuanRatio: meiTuanRatio,
          platformCouponRatio: platformCouponRatio,
        })
        //饼图
        var payListPie = [
        {
          value: res.data.result.realWePaySales,
          name: '微信'
        },
        {
          value: res.data.result.balanceSales,
          name: '余额'
        },
        {
          value: res.data.result.douYinSales,
          name: '抖音'
        },
        {
          value: res.data.result.meiTuanSales,
          name: '美团'
        },
        {
          value: res.data.result.platformCouponSales,
          name: '消费券'
        }]
        optionPie.series[0].data = payListPie;
        if (chartPie) {
          chartPie.setOption(optionPie);
        }
        //柱图
        var wePayIndexValue = [];
        var balanceIndexValue = [];
        var douYinIndexValue = [];
        var memberRechargeIndexValue = [];
        var meiTuanIndexValue = [];
        var platformCouponIndexValue = [];
        var dayList = [];
        var detailListGet = [];
        wePayIndexValue = res.data.result.chartsIndexStatistics.wePayIndexValue;
        balanceIndexValue = res.data.result.chartsIndexStatistics.balanceIndexValue;
        douYinIndexValue = res.data.result.chartsIndexStatistics.douYinIndexValue;
        memberRechargeIndexValue = res.data.result.chartsIndexStatistics.memberRechargeIndexValue;
        meiTuanIndexValue = res.data.result.chartsIndexStatistics.meiTuanIndexValue;
        platformCouponIndexValue = res.data.result.chartsIndexStatistics.platformCouponIndexValue;
        if(this.data.dateType == 2){
          for(var i=1; i<13; i++){
            dayList.push(i);
          }
          var monthNum;
          if(this.data.sendDate == this.data.toYear){
            monthNum = parseInt(this.data.toMonth.substring(5, 7));
          }else{
            monthNum = 12;
          }
          for(var i=monthNum; i>=1; i--){
            var itemDay = {
              date: '',
              // memberRecharge: '',
              realWePaySales: '',
              balanceSales: '',
              douYinSales: '',
              meiTuanSales: '',
              platformCouponSales: '',
              total: '',
            }
            itemDay.date = i;
            // itemDay.memberRecharge = memberRechargeIndexValue[i-1];
            itemDay.realWePaySales = wePayIndexValue[i-1];
            itemDay.balanceSales = balanceIndexValue[i-1];
            itemDay.douYinSales = douYinIndexValue[i-1];
            itemDay.meiTuanSales = meiTuanIndexValue[i-1];
            itemDay.platformCouponSales = platformCouponIndexValue[i-1];
            // if(this.data.storeId == '1594277494833885186'){
            //   var totalNum = (parseFloat(itemDay.memberRecharge) + parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
            // }else{
            //   var totalNum = (parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
            // }
            var totalNum = (parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
            itemDay.total = Number(totalNum.toString().match(/^\d+(?:\.\d{0,2})?/));
            detailListGet.push(itemDay);
          }
        }else{
          var totalDay = parseInt(wePayIndexValue.length);
          for(var i=1; i<=totalDay; i++){
            dayList.push(i);
          }
          if(this.data.sendDate == this.data.toMonth){
            var today = parseInt(util.formatTime(new Date()).substring(8, 10));
            for(var i=today; i>=1; i--){
              var itemDay = {
                date: '',
                // memberRecharge: '',
                realWePaySales: '',
                balanceSales: '',
                douYinSales: '',
                meiTuanSales: '',
                platformCouponSales: '',
                total: '',
              }
              itemDay.date = i;
              // itemDay.memberRecharge = memberRechargeIndexValue[i-1];
              itemDay.realWePaySales = wePayIndexValue[i-1];
              itemDay.balanceSales = balanceIndexValue[i-1];
              itemDay.douYinSales = douYinIndexValue[i-1];
              itemDay.meiTuanSales = meiTuanIndexValue[i-1];
              itemDay.platformCouponSales = platformCouponIndexValue[i-1];
              // if(this.data.storeId == '1594277494833885186'){
              //   var totalNum = (parseFloat(itemDay.memberRecharge) + parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
              // }else{
              //   var totalNum = (parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
              // }
              var totalNum = (parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
              itemDay.total = Number(totalNum.toString().match(/^\d+(?:\.\d{0,2})?/));
              detailListGet.push(itemDay);
            }
          }else{
            for(var i=totalDay; i>=1; i--){
              var itemDay = {
                date: '',
                // memberRecharge: '',
                realWePaySales: '',
                balanceSales: '',
                douYinSales: '',
                meiTuanSales: '',
                platformCouponSales: '',
                total: '',
              }
              itemDay.date = i;
              // itemDay.memberRecharge = memberRechargeIndexValue[i-1];
              itemDay.realWePaySales = wePayIndexValue[i-1];
              itemDay.balanceSales = balanceIndexValue[i-1];
              itemDay.douYinSales = douYinIndexValue[i-1];
              itemDay.meiTuanSales = meiTuanIndexValue[i-1];
              itemDay.platformCouponSales = platformCouponIndexValue[i-1];
              // if(this.data.storeId == '1594277494833885186'){
              //   var totalNum = (parseFloat(itemDay.memberRecharge) + parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
              // }else{
              //   var totalNum = (parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
              // }
              var totalNum = (parseFloat(itemDay.realWePaySales) + parseFloat(itemDay.balanceSales) + parseFloat(itemDay.douYinSales) + parseFloat(itemDay.meiTuanSales) + parseFloat(itemDay.platformCouponSales)).toFixed(2);
              itemDay.total = Number(totalNum.toString().match(/^\d+(?:\.\d{0,2})?/));

              detailListGet.push(itemDay);
            }
          }
        }
        this.setData({
          detailList: detailListGet,
        })
        optionBar.xAxis.data = dayList;
        // optionBar.series[0].data = memberRechargeIndexValue;
        optionBar.series[0].data = wePayIndexValue;
        optionBar.series[1].data = balanceIndexValue;
        optionBar.series[2].data = douYinIndexValue;
        optionBar.series[3].data = meiTuanIndexValue;
        optionBar.series[4].data = platformCouponIndexValue;
        if (chartBar) {
          chartBar.setOption(optionBar);
        }
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  monthType : function(e){
    this.setData({
      dateType: 1,
      sendDate: this.data.chooseMonth,
    })
    this.getData();
  },
  yearType : function(e){
    this.setData({
      dateType: 2,
      sendDate: this.data.chooseYear,
    })
    this.getData();
  },
  /**
   * 月份选择
   */
  bindMonthChange(e) {
    this.setData({
        chooseMonth: e.detail.value,
        sendDate: e.detail.value,
    });
    this.getData();
  },
  /**
   * 年份选择
   */
  bindYearChange(e) {
    this.setData({
      chooseYear: e.detail.value,
      sendDate: e.detail.value,
    });
    this.getData();
  },
  goOrder: function (e) {
    console.log(this.data.storeName);
    console.log(this.data.storeId);
    wx.navigateTo({
      url: '/pages/orderFlow/orderFlow?storeName=' + this.data.storeName + '&storeId=' + this.data.storeId + '&dateType=' + this.data.dateType,
    })
  },
  goRechargeFlow: function (e) {
    wx.navigateTo({
      url: '/pages/rechargeFlow/rechargeFlow?storeName=' + this.data.storeName + '&storeId=' + this.data.storeId + '&dateType=' + this.data.dateType,
    })
  },
  showList : function(e){
    this.setData({
      showDetail: true,
    });
  },
  fadeList : function(e){
    this.setData({
      showDetail: false,
    });
  },
  closeview : function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var date = util.formatTime(new Date()).substring(0, 10);
    this.setData({
      storeName: options.storeName,
      storeId: options.storeId,
      chooseMonth: date.substring(0, 7),
      toMonth: date.substring(0, 7),
      chooseYear: date.substring(0, 4),
      toYear: date.substring(0, 4),
      sendDate: date.substring(0, 7),
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})